package cn.gs.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.gs.domain.LinkMan;
import cn.gs.domain.Visit;
import cn.gs.utils.HibernateUtil;
@Component(value = "selectVisitDao")
@Scope("prototype")
public class SelectVisitDaoImpl  implements SelectVisitDao {
	
	
	Session session = HibernateUtil.openSession();
	Transaction transaction = session.getTransaction();
	
	@Override
	public List<Visit> selectVisit(String cust_name, String visit_addr, int page) {
		
		
		List<Visit> lm=new ArrayList<Visit>();

		boolean custname;
		boolean visitaddr;
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (visit_addr==null||visit_addr.isEmpty()) {
			visitaddr = true;
		} else {
			visitaddr = false;
		}
		if (visitaddr && custname) {
			transaction.begin();
			Query query = session.createQuery("from Visit");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List<Visit> list = query.list();
			transaction.commit();
			session.close();
			return list;
		}	
		if((!visitaddr)&& custname) {
			transaction.begin();
			Query query = session.createQuery("from Visit where visit_addr like ?");
			query.setParameter(0, "%" + visit_addr + "%");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		
		if(visitaddr&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			System.out.println("核心list"+list);
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from Visit where visit_cust_id like ?");
				query1.setParameter(0,  a);	
				int i = (page - 1) * 3;
				query1.setFirstResult(i);
				query1.setMaxResults(3);
				List<Visit> list1=query1.list();
				for(Visit  visit:list1 ) {					
					lm.add(visit);			
				}	
			}
			transaction.commit();
			session.close();
			int i = (page - 1) * 3;//  1 2 3 4 5
			List<Visit> fn=new ArrayList<Visit>();
			for(int i1=i;i1<lm.size();i1++) {
				if(i1<i+3) {
				fn.add(lm.get(i1)) ;
				}
			}
			return fn;		
		}	
		if((!visitaddr)&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from Visit where visit_cust_id like ? and visit_addr like ? ");
				query1.setParameter(0,  a);
				query1.setParameter(1, "%"+visit_addr+"%");
				System.out.println("联系人姓名"+visit_addr);
				List<Visit> list1=query1.list();
				System.out.println("打印添加的LIST"+list1);
				for(Visit  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();
			int i = (page - 1) * 3;//  1 2 3 4 5
			List<Visit> fn=new ArrayList<Visit>();
			for(int i1=i;i1<lm.size();i1++) {
				if(i1<i+3) {
					System.out.println("添加了"+i1+"次");
				fn.add(lm.get(i1)) ;
				}
			}
			return fn;		
		}		
		return null;	
	}

	
	
	
	
	
	
	@Override
	public List<Visit> selectPageCount(String cust_name, String visit_addr) {
		List<Visit> lm=new ArrayList<Visit>();

		boolean custname;
		boolean visitaddr;
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (visit_addr==null||visit_addr.isEmpty()) {
			visitaddr = true;
		} else {
			visitaddr = false;
		}
		if (visitaddr &&custname) {
			transaction.begin();
			Query query = session.createQuery("select count(v) from Visit v");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		
		if ((!visitaddr) &&custname) {
			transaction.begin();
			Query query = session.createQuery("select count(v) from Visit v   where visit_addr like ?");
			query.setParameter(0, "%" + visit_addr + "%");
			List list = query.list();	
			transaction.commit();
			session.close();
			return list;
		}
		if (visitaddr &&(!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from Visit where visit_cust_id like ?");
				query1.setParameter(0,  a);	
				List<Visit> list1=query1.list();
				for(Visit  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();
			List l=new ArrayList();
			l.add(lm.size());		
			return l;
	}
		if((!visitaddr)&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from Visit where visit_cust_id like ? and visit_addr like ? ");
				query1.setParameter(0,  a);
				query1.setParameter(1, "%"+visit_addr+"%");
				System.out.println("联系人姓名"+visit_addr);
				List<Visit> list1=query1.list();
				System.out.println("打印添加的LIST"+list1);
				for(Visit  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();;
			List l=new ArrayList();
			l.add(lm.size());		
			return l;	
		}
		return null;
	}
	
}
